package com.naiterui.ehp.ps.sms.factory;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.encypt.MD5;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.ps.sms.exception.ExceptionCodes;
import com.naiterui.ehp.ps.sms.exception.SmsBusinessException;
import com.naiterui.ehp.ps.sms.utils.HttpRequestUtils;
import com.naiterui.ehp.ps.sms.utils.constants.SmsConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 华信短信
 * @Author gongbaoqiang
 * @Date 2020/6/15 11:09 AM
 */
@Slf4j
public class HuaxinApi {

    private static final String BASE_URL = ConfigUtil.getString(SmsConstants.API_HUAXIN_BASE_URL);
    private static final String USER_ID = "";
    private static final String ACCOUNT = ConfigUtil.getString(SmsConstants.API_HUAXIN_ACCOUNT);

    /**
     * md5加密方式，md5采用32位大写
     */
    private static final String PASSWORD = MD5.md5(ConfigUtil.getString(SmsConstants.API_HUAXIN_PASSWORD));
    /**
     * 发送任务命令，固定值
     */
    private static final String ACTION = "send";

    private static final String CHARSET_NAME = "UTF8";

    /**
     * 发送单条短信
     *  <pre>
     *      1.短信的内容，内容需要UTF-8编码，提交内容格式：内容+【签名】
     *      2.签名是公司的名字或者公司项目名称，签名字数3-8个字
     *      3.如果内容里带 # 字符的，需要把内容urlencode一下
     *  </pre>
     * @return
     * @throws BusinessException
     */
    public static String singleSend(String phone, String content) throws SmsBusinessException {

        Map<String, String> param = new HashMap<>();
        param.put("userid", USER_ID);
        param.put("account", ACCOUNT);
        param.put("password", PASSWORD);
        param.put("action", ACTION);
        param.put("mobile", phone);
        param.put("content", content);

        log.info("调用华信发送短信接口: params : {},", param.toString());
        String result = HttpRequestUtils.doPost(BASE_URL, param);
        log.info("华信短信调用返回结果：result: {}", result);
        return result;
    }

    /**
     *  <pre>
     *      1.短信的内容，内容需要UTF-8编码，提交内容格式：内容+【签名】
     *      2.签名是公司的名字或者公司项目名称，签名字数3-8个字
     *      3.如果内容里带 # 字符的，需要把内容urlencode一下
     *  </pre>
     * @param content
     * @return
     * @throws BusinessException
     */
    private static String encodeContent(String content) throws SmsBusinessException {

        if(StringUtils.isBlank(content)){
            throw new SmsBusinessException(ExceptionCodes.SMS_CONTENT_NULL);
        }
        String str = null;
        try {
            if(content.contains("#")) {
                str = URLEncoder.encode(content, CHARSET_NAME);
            } else {
                str = new String(content.getBytes(), CHARSET_NAME);
            }
        } catch (UnsupportedEncodingException e) {
            throw new SmsBusinessException(ExceptionCodes.SMS_CONTENT_ENCODE_ERROR);
        }

        return str;
    }
}
